package uiLayer;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import controlLayer.ClassCtr;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JPanel panelMainCenter;
	
	private ClassPanel classPanel;
	private SchedulePanel schedulePanel;
	
	private ClassCtr classCtr;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		
		classPanel = new ClassPanel();
		schedulePanel = new SchedulePanel();
		try{
			classCtr = new ClassCtr();
		}
		catch (Exception e) {
			
		}
		
		
		setTitle("Mentiqa Skoleadministrationssystem\r\n");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 772, 570);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		panelMainCenter = new JPanel();
		contentPane.add(panelMainCenter, BorderLayout.CENTER);
		panelMainCenter.setLayout(new BorderLayout(0, 0));
		
		JMenuBar menuBar = new JMenuBar();
		contentPane.add(menuBar, BorderLayout.NORTH);
		
		JMenu mnFag = new JMenu("Fag");
		mnFag.setEnabled(false);
		menuBar.add(mnFag);
		
		JMenuItem mnFagNyt = new JMenuItem("Nyt");
		mnFagNyt.setEnabled(false);
		mnFag.add(mnFagNyt);
		
		JMenu mnUnderviser = new JMenu("Underviser");
		mnUnderviser.setEnabled(false);
		menuBar.add(mnUnderviser);
		
		JMenuItem mnUnderviserNy = new JMenuItem("Ny");
		mnUnderviserNy.setEnabled(false);
		mnUnderviser.add(mnUnderviserNy);
		
		JMenu mnKlasse = new JMenu("Klasse");
		menuBar.add(mnKlasse);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("Vis alle");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addClassPanelToMain();
			}
		});
		mnKlasse.add(mntmNewMenuItem);
		
//		JMenuItem mnKlasseNy = new JMenuItem("Ny");
//		mnKlasseNy.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				addClassPanelToMain();
//				addClassEditPanel();
//			}
//		});
//		mnKlasse.add(mnKlasseNy);
		
//		mnKlasseRedigerFag = new JMenuItem("Rediger fag");
//		mnKlasseRedigerFag.setEnabled(false);
//		mnKlasseRedigerFag.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				addClassPanelToMain();
//				addTeachingBlockEditPanel();
//			}
//		});
//		
//		mnKlasse.add(mnKlasseRedigerFag);
		
		
		
		JMenu mnElev = new JMenu("Elev");
		mnElev.setEnabled(false);
		menuBar.add(mnElev);
		
		JMenuItem mnElevNy = new JMenuItem("Ny");
		mnElevNy.setEnabled(false);
		mnElev.add(mnElevNy);
		
		JMenu mnLokale = new JMenu("Lokale");
		mnLokale.setEnabled(false);
		menuBar.add(mnLokale);
		
		JMenuItem mnLokaleNyt = new JMenuItem("Nyt");
		mnLokaleNyt.setEnabled(false);
		mnLokale.add(mnLokaleNyt);
		
		JMenu mnSkema = new JMenu("Skema");
		menuBar.add(mnSkema);
		
		JMenuItem mntmHent = new JMenuItem("Vis alle");
		mntmHent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addSchedulePanelToMain();
			}
		});
		mnSkema.add(mntmHent);
		
		JMenuItem mnSkemaNyt = new JMenuItem("Nyt");
		mnSkemaNyt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addSchedulePanelToMain();
				addScheduleEditPanel();
			}
		});
		mnSkema.add(mnSkemaNyt);
	}

	private void addClassPanelToMain() {
		if(panelMainCenter != null) {
			panelMainCenter.removeAll();
		}
		classPanel = new ClassPanel();
		panelMainCenter.add(classPanel, BorderLayout.CENTER);
		panelMainCenter.revalidate();
	}
	
	private void addClassEditPanel() {
		
		try {
			classPanel.getSplitPane().setRightComponent(new ClassEditPanel(classPanel, classCtr, null));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		panelMainCenter.revalidate();
	}
	
//	private void addTeachingBlockEditPanel() {
//		classPanel.getSplitPane().setRightComponent(new TeachingBlockEditPanel(null));
//		panelMainCenter.revalidate();
//	}
	
	private void addSchedulePanelToMain() {
		if(panelMainCenter != null) {
			panelMainCenter.removeAll();
		}
		schedulePanel = new SchedulePanel();
		panelMainCenter.add(schedulePanel);
		contentPane.revalidate();
	}

	private void addScheduleEditPanel() {
		schedulePanel.getSplitPane().setRightComponent(new ScheduleEditPanel(null, null));
		panelMainCenter.revalidate();
	}

}
